#include "CState.h"
using namespace Logic;
using namespace Content;
CState::CState()
{

}
CState::~CState()
{
  delete pHitBox1;
  delete pHitSphere1;
  cleanPhysics();
}


void CState::init()
{
  pInput = getInput();


  int gForce = createGravityForce(0.0f,-10.0f,0.0f);

  pHitBox1 = new Content::CHitBox(0.7f,0.8f,1.5f);
  pHitBox1->setPos(0.0f,0.0f,0.0f);
  pHitBox1->setColor(0,0,255);
  pHitBox1->updateObject(1.0f);



  pHitSphere1 = new Content::CHitSphere(1.0f);
  pHitSphere1->setPos(10,0,10);
  pHitSphere1->setColor(255,0,0);
  pHitSphere1->updateObject(1.0f); 

  pForkEntity = createEntity(0.0f, 5.0f, 0.0f);
  pForkEntity->createBoundingBox(0.7f,0.8f,1.5f);

  pMesh = createMeshFromXFile("agent.x");
	pMesh->SetAnimation(0);
  pMesh->setPos(0.0f,5.0f, 0.0f);
  
  pWorldMesh = createMeshFromXFile("world.x");
	pWorldMesh->SetAnimation(0);
  pWorldMesh->setPos(-5.0f, -0.5f, -5.0f);


  IForceRegister* pFR = getForceRegister();

  pFR->add(pHitBox1->getEntity(),gForce);
  pFR->add(pHitSphere1->getEntity(),gForce);

  pFR->add(pForkEntity,gForce);
  

  objects.push_back(pHitBox1);
  objects.push_back(pHitSphere1);


}
void CState::updateAllObjects(float alpha)
{

  for (unsigned int i = 0; i < objects.size(); ++i)
  {
    objects[i]->updateObject(alpha);
  }
  float x,y,z;
  x = 0.0f;
  y = 0.0f;
  z = 0.0f;
  pForkEntity->getPosition(x,y,z);
  pMesh->setPos(x+0.35f,y,z+0.4f);
}


void CState::addGameObject(CGameObject* pObj)
{
  objects.push_back(pObj);
}

void CState::removeGameObject(CGameObject* pObj)
{
  std::vector<CGameObject*>::iterator it = 
    std::find(objects.begin(),objects.end(),pObj);
  objects.erase(it);
}

void CState::handleInput(float dT)
{
	
    if (pInput->isKeyDown(0xC8))//felf
    {
      float yaw = pMesh->getYaw();
      float movX = -sin(yaw)*dT*1.0f;
      float movZ = -cos(yaw)*dT*1.0f;
      pMesh->moveVec(movX,0.0f,movZ);
      pForkEntity->moveVector(movX,0.0f,movZ);
      pMesh->SetAnimation(0);
    }
    else if (pInput->isKeyDown(0xD0)) //le
    {
      float yaw = pMesh->getYaw();
      float movX = -sin(yaw)*dT*1.0f;
      float movZ = -cos(yaw)*dT*1.0f;
      pMesh->moveVec(-movX,0.0f,-movZ);
      pForkEntity->moveVector(-movX,0.0f,-movZ);
      pMesh->SetAnimation(0);
    }
    else
    {
      pMesh->SetAnimation(1);
    }

    if (pInput->isKeyDown(0xCB)) //bal
    {
      pMesh->yaw(-dT*2.0f);
    }
    if (pInput->isKeyDown(0xCD))//jobb
    { 
      pMesh->yaw(dT*2.0f);
    }
	if(pInput->isKeyPressed(0x36))
	{
		pForkEntity->addForce(0,1000,0);
	}

}